# @Time    :2025/4/16 19:14

# def longestCommonPrefix(strs) :
#     if not strs:
#         return ""
#     str0 = min(strs)
#     str1 = max(strs)
#     for i in range(len(str0)):  # 遍历最短数组str0，避免索引超出范围
#         # 如果在某个位置 i 上，str0 和 str1 的字符不相等，则返回 str0 从开始到该位置 i 的子串（不包括 i 位置的字符），这代表了最长公共前缀
#         if str0[i] != str1[i]:
#             return str0[:i]
#     return str0
#
# strs = ["flower", "flow", "flight"]




def candy(ratings):
    n = len(ratings)

    candies = [1] * n  # 初始化每个孩子的糖果数为1
    # 从左到右扫描
    for i in range(1, n):
        if ratings[i] > ratings[i - 1]:
            candies[i] = candies[i - 1] + 1

            # 从右往左扫描
    for i in range(n - 2, -1, -1):
        if ratings[i] > ratings[i + 1]:
            candies[i] = max(candies[i], candies[i + 1] + 1)
            # 计算总糖果数
    return sum(candies)

ratings = [1,0,2]
print(candy(ratings))